app.controller('BusPriceScheduleListCtrl', function ($scope, ajax, $modal, toaster, $stateParams) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.typeList = [
		{ id: 0, name: "无折扣" },
		{ id: 1, name: "等额递减" },
		{ id: 2, name: "等差递减" }
	];

	$scope.tpl = null;
	$scope.tplId = $stateParams.tplId;
	$scope.routeId = $stateParams.routeId;
	ajax.call(new Req.GET({
		resource: 'shift/bs/tpl/' + $scope.tplId
	})).then(function (result) {
		if (result.code == 200) $scope.tpl = result.result;
	});

	$scope.reload = $scope.page.loader('price/bs/schedule/{tplId}/{pageIndex}/{pageSize}/page'.format({ tplId: $scope.tplId }));
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);
	$scope.queryMap = {};
	$scope.query = function () { $scope.reload(1, $scope.queryMap); };

	$scope.query = function () {
		if ($scope.typeList.selected != undefined) {
			$scope.queryMap.type = $scope.typeList.selected.id;
		} else if ($scope.typeList.selected == undefined) {
			delete $scope.queryMap.type;
		}
		$scope.reload(1, $scope.queryMap);
	};
	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		$modal.open({
			templateUrl: 'template/bus/priceSchedule/writeModal.html?v=01be5186b4',
			controller: 'BusPriceScheduleModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type,
						"tplId": $scope.tplId,
						"routeId": $scope.routeId
					};
				}
			}
		}).result.then(function (param) {
			if (param.type == 'update') {
				updater.datalist = param.ids || [];
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'price/bs/schedule/update';
				$scope.page.mod(updater, $scope.reload, $scope.queryMap);
			} else if (param.type == 'insert') {
				inserter.datalist = param.ids || [];
				inserter.fields = param.data;
				inserter.resource = 'price/bs/schedule/insert';
				$scope.page.add(inserter, $scope.reload, $scope.queryMap);
			}
		});
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		$scope.delModal(size, current_data, submit_type).result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'price/bs/schedule/' + deleter.id + '/delete';
				ajax.call(new Req.POST({
					fields: param.data,
					resource: 'price/bs/schedule/shifts'
				})).then(function (result) {
					if (result.code == 200 && result.result.length > 0) {
						$modal.open({
							templateUrl: 'template/bus/priceSchedule/syncShift.html?v=01be5186b4',
							controller: 'BusPriceSyncShiftsCtrl',
							size: 'lg',
							resolve: {
								param: function () {
									return {
										"list": result.result
									};
								}
							}
						}).result.then(function (params) {
							deleter.datalist = params || [];
							$scope.page.del(deleter, $scope.reload, $scope.queryMap);
						}, function () {
							$scope.page.del(deleter, $scope.reload, $scope.queryMap);
						});
					} else if (result.code != 423) {
						$scope.page.del(deleter, $scope.reload, $scope.queryMap);
					}
				}, function () {
					$scope.page.del(deleter, $scope.reload, $scope.queryMap);
				});
			}
		});
	};

});
